package com.bawei.image

import android.content.Context
import android.content.res.Resources
import android.net.Uri
import android.widget.ImageView
import com.bawei.image.loader.CustomImageLoader
import com.bawei.image.loader.FrescoImageLoader
import com.bawei.image.loader.GlideImageLoader
import com.bawei.image.loader.PicassoImageLoader
import com.bawei.image.loader.UniversalImageLoader
import java.io.File

/**
 * Author: Jason Zhang
 * Date: 2024/9/13
 * Desc: 图片加载管理器
 */
class LoadImageManager constructor(private val ctx: Context){

    private var imgLoader: IImageLoader =  GlideImageLoader()

    private var mOptions = ImageOptions.build()

    private var mUrl: String? = null
    private var mResource: Int? = null
    private var mFile: File? = null
    private var mUri: Uri? =  null
    private var mByteArray: ByteArray? = null

    private var resourceType = ResourceType.URL

    fun switchStrategy(strategy: ImageLoadStrategy): LoadImageManager{
        imgLoader = when(strategy){
            ImageLoadStrategy.GLIDE -> GlideImageLoader()
            ImageLoadStrategy.PICASSO -> PicassoImageLoader()
            ImageLoadStrategy.FRESCO -> FrescoImageLoader()
            ImageLoadStrategy.UNIVERSAL -> UniversalImageLoader()
            ImageLoadStrategy.CUSTOM -> CustomImageLoader()
        }
        return this
    }

    fun setOptions(options: ImageOptions): LoadImageManager{
        mOptions = options
        return this
    }

    fun loadFile(file: File): LoadImageManager{
        mFile = file
        resourceType = ResourceType.FILE
        return this
    }

    fun loadUrl(url: String): LoadImageManager{
        mUrl = url
        resourceType = ResourceType.URL
        return this
    }

    fun loadUri(uri: Uri): LoadImageManager{
        mUri = uri
        resourceType = ResourceType.URI
        return this
    }

    fun loadResource(resId: Int):LoadImageManager{
        mResource = resId
        resourceType = ResourceType.RESOURCE
        return this
    }

    fun loadByteArray(byteArray: ByteArray):LoadImageManager{
        mByteArray = byteArray
        resourceType = ResourceType.BYTEARRAY
        return this
    }

    fun into(target: ImageView){
        when(resourceType){
            ResourceType.FILE -> mFile?.let {
                imgLoader.loadFileIntoImageView(ctx, it, target, mOptions)
            }
            ResourceType.URL -> mUrl?.let {
                imgLoader.loadUrlIntoImageView(ctx, it, target, mOptions)
            }
            ResourceType.URI -> mUri?.let {
                imgLoader.loadUriIntoImageView(ctx, it, target, mOptions)
            }
            ResourceType.RESOURCE -> mResource?.let {
                imgLoader.loadResourceIntoImageView(ctx, it, target, mOptions)
            }
            ResourceType.BYTEARRAY -> mByteArray?.let {
                imgLoader.loadByteArrayIntoImageView(ctx, it, target, mOptions)
            }
        }
    }

    enum class ResourceType{
        FILE, URL, URI, RESOURCE, BYTEARRAY
    }
}